Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

题目大意:给定一个数独,判断该数独是否可用,未填数字的地方,用'.'代替

题目难度:Easy

/**
 * Created by gzdaijie on 16/5/8
 * 每次遇到数字board[i][j],将第i行,第j列,第i/3*3 + j/3块的这个数字标记为出现过
 * 一旦矛盾,说明数独不可解
 */
public class Solution {
    public boolean isValidSudoku(char[][] board) {
        Boolean[][] row = new Boolean[9][9];
        Boolean[][] col = new Boolean[9][9];
        Boolean[][] block = new Boolean[9][9];

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.') {
                    int t = board[i][j] - '1';
                    // 初始值为null,并不是false
                    if (row[i][t] == null && col[j][t] == null && block[i/3 * 3 + j/3][t] == null) {
                        row[i][t] = col[j][t] = block[i/3 * 3 + j/3][t] = true;
                    } else {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}
gzdaijie            updated 2016-05-08 17:51:16

results matching ""

    No results matching ""